<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$kwtree_bin = "python /home/luanjunyi/yhhd/py/taras/keyword_tree.py ";

if ($_POST) {
  if (isset($_GET['submit'])) {
    $back_up = "tmp/kwtree.xml." . date("Y-m-d-h:i:s");
    //echo sprintf("mv res/kwtree.xml %s<br/>", $back_up);
    system(sprintf("mv res/kwtree.xml %s", $back_up));
    //echo sprintf("cp %s res/kwtree.xml<br/>", $_POST['tree_path']);
    system(sprintf("cp %s res/kwtree.xml", $_POST['tree_path']));

    $txt_back_up = "tmp/tree.txt." . date("Y-m-d-h:i:s");
    system(sprintf("mv res/tree.txt %s", $txt_back_up));
    system(sprintf("cp %s res/tree.txt", $_POST['txt_tree_path']));


    echo sprintf("已经更新了，旧的在%s备份，保留到今天的最后一秒~", $back_up);
    return;
  }

  if (isset($_FILES["tree-file"]) && $_FILES["tree-file"]["error"] <= 0) {
      move_uploaded_file($_FILES["tree-file"]["tmp_name"],
                         "tmp/" . $_FILES["tree-file"]["name"]);
      $output = sprintf("/www/taras-ui/tmp/kw.%d.xml", time());
      $cmd = sprintf($kwtree_bin . "%s %s 2>&1",
                    "/www/taras-ui/tmp/" . $_FILES["tree-file"]["name"], $output);

      $tree_path = substr($output, strlen("/www/taras-ui/"));
      $txt_tree_path = "/www/taras-ui/tmp/" . $_FILES["tree-file"]["name"];

      session_start();

      exec($cmd, $result);
      foreach ($result as $out) {
        echo $out . "</br>";
      }
?>

<html>
  <head>
    <script type="text/javascript" src="js/jquery.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">
    <script type="text/javascript" src="js/sprintf.js"></script>
    <script type="text/javascript" src="js/jquery.jstree.js"></script>
    <script type="text/javascript">
    function submit_tree() {
        var r = confirm('如果提交，将更新服务器上用于标注网站的关键词树，真的要这样吗?');
        if (r == true) {
            $("#submit_stat").load('test_kwtree.php?submit',
                                   {'tree_path': "<?php echo $tree_path ?>",
                                    'txt_tree_path': "<?php echo $txt_tree_path ?>"});
            } else {
                $("#submit_stat").html('未提交');
            }
    }

    $(document).ready(function() {
        try {
            // load keyword tree
            $.jstree._themes = "jstree-themes/";

            $("div#kw_tree").bind("loaded.jstree", function(event, data) {

            }).jstree({
                "core": {
                    "open_parents": false,
                    "rtl": false
                },

                "xml_data": {
                    "ajax": { "url" : "<?php echo $tree_path?>",
                              success: function() {
                              }
                            },
                    "xsl": "nest"
                },

                "themes": {
                    "theme": "apple"
                },

                plugins: ["xml_data", "themes"]
            });

            $("#change_tree").click(submit_tree);


        } catch(err) {
            alert('error on page loading: ' + err);
        }
    });
   </script>

   <style>
       #kw_tree {
      width: 70%;
       }
   </style>

    <title>关键词树测试页面</title>
  </head>

  <body>
    <div id="kw_tree"></div>
    <button id="change_tree">提交此树</button>
    <span id="submit_stat"></span>
  </body>
</html>

<?php
   } else {
      echo "Error: " . $_FILES["tree-file"]["error"] . "<br />";
      return;
   }
}
else {
?>


<html>
  <head>
    <title>测试你的关键词树</title>
    <script type="text/javascript" src="/jquery-1.5.2.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">
  </head>

  <body>
    <center>
      <form action="test_kwtree.php" method="post" enctype="multipart/form-data">
        <label for="tree-file">关键词文件:</label>
        <input type="file" name="tree-file" id="tree-file" value="" /> 
        <input type="submit" name="submit" value="提交" />
      </form>

      <div id="kw_tree"></div>
    </center>
  </body>

</html>

<?php
}
?>
